package com.rainbow.service;

import com.rainbow.dao.UserDao;
import com.rainbow.domain.Order;
import com.rainbow.domain.User;
import com.rainbow.utils.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "shop-user",topic = "order-topic"
//消费模式:同步(d)/顺序        ,consumeMode = ConsumeMode.CONCURRENTLY
//消息模式:集群(d,1次)/广播(每消费者1次)    ,messageModel = MessageModel.CLUSTERING
)
public class SmsService  implements RocketMQListener<Order> {
    @Autowired
    private UserDao userDao;
    @Override
    public void onMessage(Order order) {
        log.info("接受到一个订单信息{},接下来发送短息",order);

        User user = userDao.findById(order.getUid()).get();
        String mobile = user.getTelephone();
        String code = String.valueOf(new Random().nextInt(89999) +10000);
        SmsUtil.sendSms(mobile,code);
    }
}
